package com.juzipi.demo.singleton.type5;

public class SingletonTest5 {

    public static void main(String[] args) {
        //懒汉式4，线程安全，同步代码块
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());
        System.out.println(instance.equals(instance1));
        System.out.println(instance == instance1);
    }

}

//懒汉式4，线程安全,同步代码块
class Singleton{
    private static Singleton instance;

    private Singleton(){

    }
    //提供一个静态的公有方法，加入了同步处理的代码块，并不能解决线程安全问题
    public static Singleton getInstance(){
        if (instance == null){
            //这样写并不能解决线程安全的问题
            synchronized (Singleton.class){
                instance = new Singleton();
            }
        }
        return instance;
    }

}
